//
//  CalendarDetailViewController.m
//  CangLongWnnianli
//
//  Created by 暗流澎涌 on 2025/7/2.
//

#import "CalendarDetailViewController.h"
#import <Masonry/Masonry.h>
// 在文件顶部添加导入
#import "ShichenYijiViewController.h"

@interface CalendarDetailViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *headerView;

@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UILabel *titleLabel;

- (NSString *)formatNongliString:(NSString *)nongliString;
- (NSString *)formatSecNongliString:(NSString *)nongliString;

@end

@implementation CalendarDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor colorWithRed:0.941 green:0.949 blue:0.961 alpha:1.0];
    
    // 头部导航
    self.headerView = [[UIView alloc] init];
    self.headerView.backgroundColor = [UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0];
    [self.view addSubview:self.headerView];
    
    // 返回按钮
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backButton setTitle:@"←" forState:UIControlStateNormal];
    [self.backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.backButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [self.backButton addTarget:self action:@selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:self.backButton];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"黄历详情";
    titleLabel.font = [UIFont boldSystemFontOfSize:18];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel = titleLabel;
    [self.headerView addSubview:self.titleLabel];
    
    // 滚动视图
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.showsVerticalScrollIndicator = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.bounces = YES;
    self.scrollView.alwaysBounceVertical = YES;
    [self.view addSubview:self.scrollView];
    
    // 内容视图
    self.contentView = [[UIView alloc] init];
    [self.scrollView addSubview:self.contentView];
    
    // 创建内容
    [self createContent];
    
    [self setupConstraints];
}

- (void)createContent {
    // 日期卡片
    UIView *dateCard = [[UIView alloc] init];
    dateCard.backgroundColor = [UIColor whiteColor];
    dateCard.layer.cornerRadius = 12;
    dateCard.layer.shadowColor = [UIColor blackColor].CGColor;
    dateCard.layer.shadowOffset = CGSizeMake(0, 2);
    dateCard.layer.shadowOpacity = 0.1;
    dateCard.layer.shadowRadius = 4;
    [self.contentView addSubview:dateCard];
    
    // 头部
    UIView *cardHeader = [[UIView alloc] init];
    cardHeader.backgroundColor = [UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0];
    cardHeader.layer.cornerRadius = 12;
    cardHeader.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
    [dateCard addSubview:cardHeader];
    
    // 农历（大标题）
    UILabel *lunarDate = [[UILabel alloc] init];
    lunarDate.font = [UIFont boldSystemFontOfSize:20];
    lunarDate.textColor = [UIColor whiteColor];
//    lunarDate.text = self.huangliData ? [self formatNongliString:self.huangliData.nongli] : @"农历九月初一";
    lunarDate.text = self.huangliData.date ;

    [cardHeader addSubview:lunarDate];
    
    // 干支
    UILabel *ganzhi = [[UILabel alloc] init];
    ganzhi.font = [UIFont systemFontOfSize:14];
    ganzhi.textColor = [UIColor colorWithRed:0.996 green:0.988 blue:0.749 alpha:1.0];
    if (self.huangliData && self.huangliData.nongli.length > 0) {
        NSString *nongli = [self formatSecNongliString:self.huangliData.nongli];
        NSArray *components = [nongli componentsSeparatedByString:@" "];
        if (components.count >= 5) {
            ganzhi.text = [NSString stringWithFormat:@"%@",  components[4]];
        } else {
            ganzhi.text = @"---";
        }
    } else {
        ganzhi.text = @"---";
    }
    [cardHeader addSubview:ganzhi];
    
    // 日期圆圈
    UIView *dayCircle = [[UIView alloc] init];
    dayCircle.backgroundColor = [UIColor colorWithRed:0.839 green:0.620 blue:0.180 alpha:1.0];
    dayCircle.layer.cornerRadius = 20;
    [cardHeader addSubview:dayCircle];
    
    UILabel *dayLabel = [[UILabel alloc] init];
    dayLabel.font = [UIFont boldSystemFontOfSize:18];
    dayLabel.textColor = [UIColor whiteColor];
    dayLabel.textAlignment = NSTextAlignmentCenter;
    if (self.huangliData && self.huangliData.date.length >= 10) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy-MM-dd";
        NSDate *date = [formatter dateFromString:self.huangliData.date];
        formatter.dateFormat = @"dd";
        dayLabel.text = [formatter stringFromDate:date];
    } else {
        dayLabel.text = @"15";
    }
    [dayCircle addSubview:dayLabel];
    [dayCircle setHidden:YES ];
    [cardHeader mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(dateCard);
        make.height.mas_equalTo(80);
    }];
    
    [lunarDate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cardHeader).offset(16);
        make.top.equalTo(cardHeader).offset(16);
    }];
    
    [ganzhi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lunarDate);
        make.top.equalTo(lunarDate.mas_bottom).offset(4);
    }];
    
    [dayCircle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(cardHeader).offset(-16);
        make.centerY.equalTo(cardHeader);
        make.width.height.mas_equalTo(40);
    }];
    
    [dayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(dayCircle);
    }];
    
    // 信息网格
    UIView *infoGrid = [[UIView alloc] init];
    [dateCard addSubview:infoGrid];
    NSString *nongli = [self formatSecNongliString:self.huangliData.nongli];
    NSArray *components = [nongli componentsSeparatedByString:@" "];
//    "date": "2016-01-01",
//    "nongli": "乙未年戊子月壬午日属羊周五第1周",
//    "yi": "沐浴 破屋 祭祀 馀事勿取 坏垣",
//    "ji": "结婚 出行 搬家 搬新房 盖屋 探病",
//    "wuxing": "杨柳木",
//    "chongsha": "冲鼠煞北",
//    "zhishen": "司命",
//    "shichen_lis": [
//      "庚子吉",
//      "辛丑吉",
//      "壬寅凶",
//      "癸卯吉",
//      "甲辰凶",
//      "乙巳凶",
//      "丙午吉",
//      "丁未凶",
//      "戊申吉",
//      "己酉吉",
//      "庚戌凶",
//      "辛亥凶"
//    ],
//    "jianshen": "破日",
//    "jishen": "月德 天恩 阳德 六仪 续世 解神 司命 鸣吠",
//    "taishen": "仓库碓外西北",
//    "xiongshen": "月破 大耗 灾煞 天火 厌对 招摇 五虚 血忌",
//    "xingxiu": "牛金牛宿星",
//    "pengzu": "壬不汲水更难提防 午不苫盖屋主更张",
    NSString * ganzhiStr = [NSString stringWithFormat:@"%@ %@ %@",  components[0], components[1], components[2]];
    NSArray *infoItems = @[
//        @{@"title": @"农历", @"value": @"九月初一"},
      
        @{@"title": @"干支", @"value": ganzhiStr},
        @{@"title": @"五行", @"value": self.huangliData.wuxing},
        @{@"title": @"冲煞", @"value": self.huangliData.chongsha},
        @{@"title": @"值神", @"value": self.huangliData.zhishen},
        @{@"title": @"建除十二神", @"value": self.huangliData.jianshen},
        @{@"title": @"吉神宜趋", @"value": self.huangliData.jishen},
        @{@"title": @"胎神", @"value": self.huangliData.taishen},
        @{@"title": @"凶神宜忌", @"value": self.huangliData.xiongshen},
        @{@"title": @"二十八星宿", @"value": self.huangliData.xingxiu},
        @{@"title": @"彭祖百忌", @"value": self.huangliData.pengzu},
    ];
    
    for (int i = 0; i < infoItems.count; i++) {
        NSDictionary *item = infoItems[i];
        
        UIView *itemView = [[UIView alloc] init];
        itemView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0];
        itemView.layer.cornerRadius = 8;
        [infoGrid addSubview:itemView];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = item[@"title"];
        titleLabel.font = [UIFont systemFontOfSize:12];
        titleLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
        [itemView addSubview:titleLabel];
        
        UILabel *valueLabel = [[UILabel alloc] init];
        valueLabel.text = item[@"value"] ?: @"暂无数据";
        valueLabel.font = [UIFont systemFontOfSize:14];
        valueLabel.textColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
        valueLabel.numberOfLines = 0;
        [itemView addSubview:valueLabel];
        
        [valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(itemView).inset(8);
            make.top.equalTo(titleLabel.mas_bottom).offset(4);
        }];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(itemView).offset(8);
        }];
        
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo((UIScreen.mainScreen.bounds.size.width - 64) / 2);
            make.height.mas_equalTo(80);
            make.left.equalTo(infoGrid).offset((i % 2) * ((UIScreen.mainScreen.bounds.size.width - 64) / 2 + 8));
            make.top.equalTo(infoGrid).offset((i / 2) * 88);
        }];
        
        // 调试"彭祖百忌"项目
        if ([item[@"title"] isEqualToString:@"彭祖百忌"]) {
            NSLog(@"彭祖百忌 - titleLabel: %@", titleLabel.text);
            NSLog(@"彭祖百忌 - valueLabel: %@", valueLabel.text);
            NSLog(@"彭祖百忌 - itemView subviews count: %lu", (unsigned long)itemView.subviews.count);
        }
    }
    
    // 调试信息：确认项目数量
    NSLog(@"infoItems count: %lu", (unsigned long)infoItems.count);
    NSLog(@"infoGrid subviews count: %lu", (unsigned long)infoGrid.subviews.count);
    
    [infoGrid mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cardHeader.mas_bottom);
        make.left.right.equalTo(dateCard).inset(16);
    }];
    
    // 设置最后一个itemView的底部约束，撑开infoGrid
    if (infoItems.count > 0) {
        // 计算最后一个itemView的位置
        int lastIndex = (int)infoItems.count - 1;
        int lastRow = lastIndex / 2;
        int lastCol = lastIndex % 2;
        
        // 找到对应的itemView（每个itemView在infoGrid.subviews中的索引是 i*2 和 i*2+1）
        UIView *lastItemView = infoGrid.subviews[lastIndex];
        
        [infoGrid mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(lastItemView.mas_bottom);
        }];
    }
    
    // 宜忌卡片
    UIView *yiJiCard = [[UIView alloc] init];
    yiJiCard.backgroundColor = [UIColor whiteColor];
    yiJiCard.layer.cornerRadius = 12;
    yiJiCard.layer.shadowColor = [UIColor blackColor].CGColor;
    yiJiCard.layer.shadowOffset = CGSizeMake(0, 2);
    yiJiCard.layer.shadowOpacity = 0.1;
    yiJiCard.layer.shadowRadius = 4;
    [self.contentView addSubview:yiJiCard];
    
    UILabel *yiJiTitle = [[UILabel alloc] init];
    yiJiTitle.text = @"今日宜忌";
    yiJiTitle.font = [UIFont boldSystemFontOfSize:18];
    yiJiTitle.textColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
    [yiJiCard addSubview:yiJiTitle];
    
    // 宜
    UILabel *yiTitle = [[UILabel alloc] init];
    yiTitle.text = @"宜";
    yiTitle.font = [UIFont boldSystemFontOfSize:16];
    yiTitle.textColor = [UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0];
    [yiJiCard addSubview:yiTitle];
    
    NSArray *yiItems = @[@"祭祀", @"出行", @"结婚", @"搬家", @"开业", @"纳财", @"求医", @"启钻"];
    UIView *yiContainer = [self createItemsContainer:yiItems color:[UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:0.1] textColor:[UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0]];
    [yiJiCard addSubview:yiContainer];
    
    // 忌
    UILabel *jiTitle = [[UILabel alloc] init];
    jiTitle.text = @"忌";
    jiTitle.font = [UIFont boldSystemFontOfSize:16];
    jiTitle.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
    [yiJiCard addSubview:jiTitle];
    
    NSArray *jiItems = @[@"动土", @"破土", @"安葬", @"修造", @"入宅", @"开光"];
    UIView *jiContainer = [self createItemsContainer:jiItems color:[UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0] textColor:[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0]];
    [yiJiCard addSubview:jiContainer];
    
    [yiJiTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(yiJiCard).offset(16);
    }];
    
    [yiTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(yiJiCard).offset(16);
        make.top.equalTo(yiJiTitle.mas_bottom).offset(16);
    }];
    
    [yiContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(yiJiCard).inset(16);
        make.top.equalTo(yiTitle.mas_bottom).offset(8);
    }];
    
    [jiTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(yiJiCard).offset(16);
        make.top.equalTo(yiContainer.mas_bottom).offset(16);
    }];
    
    [jiContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(yiJiCard).inset(16);
        make.top.equalTo(jiTitle.mas_bottom).offset(8);
        make.bottom.equalTo(yiJiCard).offset(-16);
    }];
    
    // 设置约束
    [dateCard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.contentView).inset(16);
        make.bottom.equalTo(infoGrid.mas_bottom).offset(16);
    }];
    
   
    // 在createContent方法的最后，yiJiCard约束设置之前添加：
    
    // 查看时辰宜忌按钮
    UIButton *shichenButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [shichenButton setTitle:@"查看时辰宜忌" forState:UIControlStateNormal];
    [shichenButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    shichenButton.titleLabel.font = [UIFont systemFontOfSize:16];
    shichenButton.backgroundColor = [UIColor colorWithRed:0.839 green:0.620 blue:0.180 alpha:1.0];
    shichenButton.layer.cornerRadius = 8;
    [shichenButton addTarget:self action:@selector(shichenButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:shichenButton];
    
    // 修改yiJiCard的约束，为按钮留出空间
    [yiJiCard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dateCard.mas_bottom).offset(16);
        make.left.right.equalTo(self.contentView).inset(16);
        // 移除原来的bottom约束
    }];
    
    // 添加按钮约束
    [shichenButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(yiJiCard.mas_bottom).offset(16);
        make.left.right.equalTo(self.contentView).inset(16);
        make.height.mas_equalTo(44);
        make.bottom.equalTo(self.contentView).offset(-16);
    }];
    
    // 在Actions部分添加按钮点击方法：
    
  
}
- (void)shichenButtonTapped {
    ShichenYijiViewController *shichenVC = [[ShichenYijiViewController alloc] init];
    shichenVC.huangliData = self.huangliData;
    shichenVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:shichenVC animated:YES];
}
- (UIView *)createItemsContainer:(NSArray *)items color:(UIColor *)bgColor textColor:(UIColor *)textColor {
    UIView *container = [[UIView alloc] init];
    UIView *lastLabel = nil;
    UIView *lastRowFirstLabel = nil;
    CGFloat maxWidth = UIScreen.mainScreen.bounds.size.width - 64;
    CGFloat itemHeight = 28;
    CGFloat itemSpacing = 8;
    CGFloat lineSpacing = 8;
    CGFloat currentRowWidth = 0;

    for (int i = 0; i < items.count; i++) {
        NSString *item = items[i];
        UILabel *itemLabel = [[UILabel alloc] init];
        itemLabel.text = item;
        itemLabel.font = [UIFont systemFontOfSize:14];
        itemLabel.textColor = textColor;
        itemLabel.backgroundColor = bgColor;
        itemLabel.layer.cornerRadius = 4;
        itemLabel.layer.masksToBounds = YES;
        itemLabel.textAlignment = NSTextAlignmentCenter;
        [container addSubview:itemLabel];

        CGSize textSize = [item sizeWithAttributes:@{NSFontAttributeName: itemLabel.font}];
        CGFloat itemWidth = textSize.width + 16;

        BOOL isNewLine = (!lastLabel || currentRowWidth + itemWidth > maxWidth);

        [itemLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(itemHeight);
            make.width.mas_equalTo(itemWidth);
            if (isNewLine) {
                make.left.equalTo(container);
                if (lastRowFirstLabel) {
                    make.top.equalTo(lastRowFirstLabel.mas_bottom).offset(lineSpacing);
                } else {
                    make.top.equalTo(container);
                }
            } else {
                make.left.equalTo(lastLabel.mas_right).offset(itemSpacing);
                make.top.equalTo(lastLabel);
            }
        }];

        if (isNewLine) {
            currentRowWidth = itemWidth + itemSpacing;
            lastRowFirstLabel = itemLabel;
        } else {
            currentRowWidth += itemWidth + itemSpacing;
        }
        lastLabel = itemLabel;
    }
    // 最后一行bottom
    if (lastLabel) {
        [container mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(lastLabel.mas_bottom);
        }];
    }
    return container;
}

- (void)setupConstraints {
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(88);
    }];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerView).offset(16);
      
        make.top.equalTo(self.headerView.mas_top).offset(40);
        make.width.height.mas_equalTo(44);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_top).offset(55);
        make.centerX.equalTo(self.headerView.mas_centerX).offset(0);
        make.height.mas_equalTo(20);
    }];
}

#pragma mark - Actions

- (void)backButtonTapped {
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSString *)formatNongliString:(NSString *)nongliString {
    if (!nongliString || nongliString.length == 0) {
        return nongliString;
    }
    
    // 示例: "乙巳年壬午月甲戌日属蛇周五第27周"
    // 目标: "乙巳年 壬午月 甲戌日\n属蛇 周 五 第 27周"
    
    NSMutableString *formattedString = [NSMutableString stringWithString:nongliString];
    
    // 1. 在年月日中间加空格
    // 找到"年"、"月"、"日"的位置并插入空格
    NSRange yearRange = [formattedString rangeOfString:@"年"];
    if (yearRange.location != NSNotFound) {
        [formattedString insertString:@" " atIndex:yearRange.location + 1];
    }
    
    NSRange monthRange = [formattedString rangeOfString:@"月"];
    if (monthRange.location != NSNotFound) {
        [formattedString insertString:@" " atIndex:monthRange.location + 1];
    }
    
    NSRange dayRange = [formattedString rangeOfString:@"日"];
    if (dayRange.location != NSNotFound) {
        // 在"日"后面换行
        [formattedString insertString:@"\n" atIndex:dayRange.location + 1];
    }
    
    // 2. 在第一个"周"字前加空格
    NSRange firstWeekRange = [formattedString rangeOfString:@"周"];
    if (firstWeekRange.location != NSNotFound) {
        [formattedString insertString:@" " atIndex:firstWeekRange.location];
    }
    
    // 3. 在"第"字前加空格
    NSRange diRange = [formattedString rangeOfString:@"第"];
    if (diRange.location != NSNotFound) {
        [formattedString insertString:@" " atIndex:diRange.location];
    }
    
    return formattedString;
}

- (NSString *)formatSecNongliString:(NSString *)nongliString {
    if (!nongliString || nongliString.length == 0) {
        return nongliString;
    }
    
    // 示例: "乙巳年壬午月甲戌日属蛇周五第27周"
    // 目标: "乙巳年 壬午月 甲戌日\n属蛇 周 五 第 27周"
    
    NSMutableString *formattedString = [NSMutableString stringWithString:nongliString];
    
    // 1. 在年月日中间加空格
    // 找到"年"、"月"、"日"的位置并插入空格
    NSRange yearRange = [formattedString rangeOfString:@"年"];
    if (yearRange.location != NSNotFound) {
        [formattedString insertString:@" " atIndex:yearRange.location + 1];
    }
    
    NSRange monthRange = [formattedString rangeOfString:@"月"];
    if (monthRange.location != NSNotFound) {
        [formattedString insertString:@" " atIndex:monthRange.location + 1];
    }
    
    NSRange dayRange = [formattedString rangeOfString:@"日"];
    if (dayRange.location != NSNotFound) {
        // 在"日"后面换行
        [formattedString insertString:@" " atIndex:dayRange.location + 1];
    }
    
    // 2. 在第一个"周"字前加空格
    NSRange firstWeekRange = [formattedString rangeOfString:@"周"];
    if (firstWeekRange.location != NSNotFound) {
        [formattedString insertString:@" " atIndex:firstWeekRange.location];
    }
    
    // 3. 在"第"字前加空格
    NSRange diRange = [formattedString rangeOfString:@"第"];
    if (diRange.location != NSNotFound) {
        [formattedString insertString:@" " atIndex:diRange.location];
    }
    
    return formattedString;
}

@end
